Passed
Push — master ( 88b071...71ea90 )
by Guangyu
09:22 queued 12s
created

MultipleLineChart.js ➔ minValue   A

Complexity

Conditions 2

Size

Total Lines 11
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 11
rs 9.95
c 0
b 0
f 0
cc 2
1
import React, { useState, useContext, useEffect, useRef } from 'react';
2
import { Row, Col, Card, CardBody } from 'reactstrap';
3
import { CheckPicker } from 'rsuite';
4
import { rgbaColor, themeColors, isIterableArray } from '../../../helpers/utils';
5
import AppContext from '../../../context/Context';
6
import moment from 'moment';
7
import ReactEchartsCore from 'echarts-for-react/lib/core';
8
import * as echarts from 'echarts/lib/echarts';
9
import { LineChart } from 'echarts/charts';
10
import { 
11
  GridComponent,
12
  ToolboxComponent, 
13
  DataZoomComponent,
14
  MarkLineComponent, 
15
  MarkPointComponent} from 'echarts/components';
16
17
echarts.use([
18
  LineChart, 
19
  GridComponent, 
20
  ToolboxComponent, 
21
  DataZoomComponent, 
22
  MarkLineComponent, 
23
  MarkPointComponent]);
24
25
const MultipleLineChart = ({
26
  reportingTitle,
27
  baseTitle,
28
  labels,
29
  data,
30
  options
31
}) => {
32
  const [values, setValues] = useState(['a0']);
33
  const [oldValues, setOldValues] = useState(['a0']);
34
  const { isDark } = useContext(AppContext);
35
  const [nodes, setNodes] = useState([{
36
    name: options.label,
37
    borderWidth: 2,
38
    data: data['a0'],
39
    type: 'line',
40
    markPoint: {
41
      label:{
42
        color: rgbaColor('#fff', 0.8),
43
      },
44
      data: [
45
        {
46
          type: 'max',
47
          name: 'Max Value'
48
        },
49
        {
50
          type: 'min',
51
          name: 'Min Value'
52
        }
53
      ]
54
    },
55
    markLine: {
56
      data: [{
57
          type: 'average',
58
          name: 'Average Value'
59
      }],
60
      label:{
61
        color: rgbaColor('#fff', 0.8),
62
      }
63
    },
64
  }]);
65
  const [lastMoment, setLastMoment] = useState(moment());
66
  const [lineLabels, setLinaLabels] = useState([]);
67
68
  let handleChange = (arr) => {
69
    if (arr.length < 1) {
70
      return ;
71
    }
72
    let currentMoment = moment();
73
    setOldValues(values);
74
    setValues(arr);
75
    setLastMoment(currentMoment);
76
  }
77
78
  useEffect(() => {
79
    let tempNodes = [...nodes];
80
    if (options[0] && data['a0'] && tempNodes.length > 0 && tempNodes[0].label === undefined) {
81
      let index = values[0];
82
      tempNodes = [];
83
      tempNodes[0] = {
84
        data: data[index],
85
        type: 'line',
86
        smooth: true,
87
        name: options[index.slice(1)].label,
88
        itemStyle: {
89
          lineStyle: {
90
            color: rgbaColor("#"+((1<<24)*Math.random()|0).toString(16), 0.8),
91
          }
92
        },
93
        markPoint: {
94
          data: [
95
           {
96
              type: 'max',
97
              name: 'Max Value'
98
            },
99
            {
100
              type: 'min',
101
              name: 'Min Value'
102
            }
103
          ],
104
          label:{
105
            color: rgbaColor('#fff', 0.8),
106
          }
107
        },
108
        markLine: {
109
          data: [{
110
              type: 'average',
111
              name: 'Average Value'
112
          }],
113
          label:{
114
            color: rgbaColor('#fff', 0.8),
115
          }
116
        },
117
      }
118
    }
119
    setNodes(tempNodes);
120
    setLinaLabels(labels[values[0]]);
121
    setValues(['a0']);
122
    setOldValues(['a0'])
123
  }, [data, labels]);
124
125
  useEffect(() => {
126
    let tempNodes = [...nodes];
127
    if (oldValues.length < values.length) {
128
      let index = values[values.length - 1];
129
      tempNodes.push({
130
        data: data[index],
131
        type: 'line',
132
        smooth: true,
133
        name: options[index.slice(1)].label,
134
        itemStyle: {
135
          lineStyle: {
136
            color: rgbaColor("#"+((1<<24)*Math.random()|0).toString(16), 0.8),
137
          }
138
        },
139
        markPoint: {
140
          data: [
141
            {
142
              type: 'max',
143
              name: 'Max Value'
144
            },
145
            {
146
              type: 'min',
147
              name: 'Min Value'
148
            }
149
          ],
150
          label:{
151
            color: rgbaColor('#fff', 0.8),
152
          }
153
        },
154
        markLine: {
155
          data: [
156
            {
157
              type: 'average',
158
              name: 'Average Value'
159
            }
160
          ],
161
          label:{
162
            color: rgbaColor('#fff', 0.8),
163
          }
164
        },
165
      })
166
    } else {
167
      let i = 0
168
      for (; i <= oldValues.length; i++ ) {
169
        if (i === values.length || oldValues[i] !== values[i]){
170
          break;
171
        }
172
      }
173
      tempNodes.splice(i, 1);
174
    }    
175
    setNodes(tempNodes);
176
    console.log(tempNodes)
177
    setLinaLabels(labels[values[0]]);
178
  }, [lastMoment]);
179
180
  let getOption = () => {
181
    return {
182
      tooltip: {
183
        trigger: 'axis'
184
      },
185
      grid: {
186
        left: '2%',
187
        right: '2%',
188
        bottom: '15%',
189
        containLabel: true
190
      },
191
      xAxis: {
192
        type: 'category',
193
        data: lineLabels ? lineLabels : ['0'],
194
        axisLabel: {
195
          interval: 'auto',
196
          textStyle: {
197
            color: rgbaColor('#fff', 0.8),
198
          }
199
        },
200
        axisLine:{
201
          lineStyle:{
202
            color: rgbaColor('#000', 0.1),
203
          }
204
        }
205
      },
206
      yAxis: {
207
        type: 'value',
208
        splitLine: {show: false},
209
        axisLabel: {
210
          textStyle: {
211
            color: rgbaColor('#fff', 0.8),
212
          }
213
        },
214
        axisLine:{
215
          lineStyle:{
216
            color: rgbaColor('#000', 0.1),
217
          }
218
        }
219
      },
220
      series: nodes,
221
      toolbox: {
222
        right: 10,
223
        feature: {
224
          dataZoom: {
225
            yAxisIndex: 'none'
226
          },
227
        },
228
        show: false
229
      },
230
      dataZoom: [
231
        {
232
         
233
        }
234
      ],
235
    };
236
  }
237
238
  return (
239
    <Card className="mb-3">
240
      <CardBody className="rounded-soft">
241
        <Row className="text-white align-items-center no-gutters">
242
          <Col>
243
            <h4 className="text-lightSlateGray mb-0">{reportingTitle}</h4>
244
            <p className="fs--1 font-weight-semi-bold">
245
              {baseTitle}
246
            </p>
247
          </Col>
248
          {options[0] && isIterableArray(options) &&
249
            <Col xs="auto" className="d-none d-sm-block">
250
              <CheckPicker
251
                data={options}
252
                value={values}
253
                appearance="default"
254
                placeholder="select"
255
                searchable={false}
256
                countable={false}
257
                onSelect={handleChange}
258
                style={{ width: 224, borderRadius: '.25rem'}}
259
                />
260
            </Col>
261
          }
262
        </Row>
263
        <ReactEchartsCore
264
            echarts={echarts}
265
            notMerge={true}
266
            option={getOption()}
267
            style={{ width: '100%', height: 318 }}
268
            />
269
      </CardBody>
270
    </Card>
271
  );
272
};
273
274
export default MultipleLineChart;